package com.crm.crm.controller;

import com.crm.crm.domain.SysDictData;
import com.crm.crm.domain.SysDictType;
import com.crm.crm.domain.dto.dict.DictDataDto;
import com.crm.crm.domain.dto.dict.DictTypeDto;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.domain.vo.dict.DictDataVo;
import com.crm.crm.domain.vo.dict.DictTypeVo;
import com.crm.crm.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/dict")
public class DictController {

    @Autowired
    private DictService dictService;


    /**
     * 获取指定字典名称对应的数据列表
     *
     * @return
     */
    @GetMapping("/data/type/{dictType}")
    public Result getDictDataType(@PathVariable String dictType) {
        return Result.success(dictService.getDictData(dictType));
    }

    /**
     * 获取字典类型分页
     *
     * @return
     */
    @GetMapping("/type/list")
    public Result getDictTypePage(DictTypeDto dictTypeDto) {
        if (dictTypeDto.getPageNum() != null && dictTypeDto.getPageSize() != null) {
            // 分页查询逻辑
            DictTypeVo pageResult = dictService.getDictTypePage(dictTypeDto);
            return Result.success().put("total", pageResult.getTotal())
                    .put("rows", pageResult.getRows());
        } else {
            DictTypeDto typeDto = new DictTypeDto();
            typeDto.setPageNum(1);
            typeDto.setPageSize(100);
            // 全量查询逻辑
            DictTypeVo pageResult = dictService.getDictTypePage(typeDto);
            return Result.success().put("total", pageResult.getTotal())
                    .put("rows", pageResult.getRows());
        }
    }

    /**
     * 添加字典类型
     *
     * @param sysDictType
     * @return
     */
    @PostMapping("/type")
    public Result addDictType(@RequestBody SysDictType sysDictType) {
        boolean flag = dictService.addDictType(sysDictType);
        if (!flag) {
            return Result.error("添加字典类型失败");
        } else {
            return Result.success();
        }
    }

    /**
     * 根据字典类型id获取字典类型
     *
     * @param dictId
     * @return
     */
    @GetMapping("/type/{dictId}")
    public Result getDictTypeById(@PathVariable Long dictId) {
        SysDictType sysDictType = dictService.getDictTypeById(dictId);
        return Result.success(sysDictType);
    }

    /**
     * 获取字典数据分页
     *
     * @return
     */
    @GetMapping("/data/list")
    public Result getDictDataPage(DictDataDto dictDataDto) {
        // 分页查询逻辑
        DictDataVo pageResult = dictService.getDictDataList(dictDataDto);
        return Result.success().put("total", pageResult.getTotal())
                .put("rows", pageResult.getRows());
    }

    /**
     * 添加字典内容
     *
     * @param sysDictData
     * @return
     */
    @PostMapping("/data")
    public Result addDictData(@RequestBody SysDictData sysDictData) {
        boolean flag = dictService.addDictData(sysDictData);
        if (!flag) {
            return Result.error("添加字典类型失败");
        } else {
            return Result.success();
        }
    }

    /**
     * 修改字典类型
     *
     * @param sysDictType
     * @return
     */
    @PutMapping("/type")
    public Result updateDictType(@RequestBody SysDictType sysDictType) {
        boolean flag = dictService.updateDictType(sysDictType);
        if (!flag) {
            return Result.error("修改字典类型失败");
        } else {
            return Result.success();
        }
    }

    /**
     * 删除字典类型
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/type/{ids}")
    public Result deleteDictType(@PathVariable String[] ids) {
        boolean flag = dictService.deleteDictType(ids);
        if (!flag) {
            return Result.error("删除字典类型失败");
        } else {
            return Result.success();
        }
    }

    /**
     * 根据id查询字典数据信息
     *
     * @param dictCode 字典类型
     * @return 字典数据集合信息
     */
    @GetMapping("/data/{dictCode}")
    public Result getDictDataById(@PathVariable Long dictCode) {
        SysDictData sysDictData = dictService.getDictDataById(dictCode);
        return Result.success(sysDictData);
    }

    /**
     * 修改字典数据信息
     *
     * @param sysDictData 字典数据信息
     * @return 结果
     */
    @PutMapping("/data")
    public Result updateDictData(@RequestBody SysDictData sysDictData) {
        if (dictService.updateDictData(sysDictData)) {
            return Result.success();
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 删除字典数据信息
     *
     * @param ids 字典数据信息
     * @return 结果
     */
    @DeleteMapping("/data/{ids}")
    public Result deleteDictData(@PathVariable String[] ids) {
        boolean flag = dictService.deleteDictData(ids);
        if (!flag) {
            return Result.error("删除字典内容失败");
        } else {
            return Result.success();
        }
    }

    /**
     * 删除缓存
     *
     * @return
     */
    @DeleteMapping("/type/clearCache")
    public Result clearCache() {
        dictService.clearCache();
        return Result.success();
    }
}
